#region License Information (GNU GPL v3)

/*
    Super Meat Boy Stats
    Copyright (C) 2011 Juzz

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#endregion License Information (GNU GPL v3)

using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Media;
using System.Windows.Forms;
using HelpersLibrary;
using SuperMeatBoyLibrary;

namespace SMBStats
{
    public partial class UploadForm : Form
    {
        private const string WebURL = "http://smbstats.muffinz.eu";

        private SMBStatsWeb web;

        public UploadForm(SMBManager smbManager)
        {
            InitializeComponent();
            Icon = Properties.Resources.smb;
            web = new SMBStatsWeb(smbManager);

            if (!string.IsNullOrEmpty(Program.Settings.UserKey))
            {
                txtUserKey.Text = Program.Settings.UserKey;
            }
        }

        private void btnRegister_Click(object sender, EventArgs e)
        {
            Process.Start(WebURL);
        }

        private void btnUpload_Click(object sender, EventArgs e)
        {
            string userKey = txtUserKey.Text;

            if (string.IsNullOrEmpty(userKey))
            {
                MessageBox.Show("User key is required.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            else
            {
                Program.Settings.UserKey = userKey;
                btnUpload.Enabled = false;
                web.UserKey = userKey.Trim();
                lblStatus.Text = "Status: Uploading...";

                BackgroundWorker bw = new BackgroundWorker();
                bw.DoWork += new DoWorkEventHandler(bw_DoWork);
                bw.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bw_RunWorkerCompleted);
                bw.RunWorkerAsync();
            }
        }

        private void bw_DoWork(object sender, DoWorkEventArgs e)
        {
            try
            {
                e.Result = web.Upload();
            }
            catch (Exception ex)
            {
                e.Cancel = true;
                MessageBox.Show(ex.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void bw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            if (!e.Cancelled)
            {
                string result = e.Result as string;

                if (!string.IsNullOrEmpty(result))
                {
                    if (result.IsValidUrl())
                    {
                        txtResult.Text = result;
                        btnCopy.Enabled = btnOpen.Enabled = true;
                        lblStatus.Text = "Status: Upload success.";
                        SystemSounds.Exclamation.Play();
                        return;
                    }
                    else
                    {
                        MessageBox.Show(result, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }
            }

            lblStatus.Text = "Status: Upload failed.";
            btnUpload.Enabled = true;
        }

        private void btnCopy_Click(object sender, EventArgs e)
        {
            string result = txtResult.Text;

            if (!string.IsNullOrEmpty(result))
            {
                Clipboard.SetText(result);
            }
        }

        private void btnOpen_Click(object sender, EventArgs e)
        {
            string result = txtResult.Text;

            if (!string.IsNullOrEmpty(result))
            {
                Process.Start(result);
            }
        }
    }
}